//
//  LMRegistController.m
//  EPA
//
//  Created by lanou3g on 16/4/26.
//  Copyright © 2016年 liman. All rights reserved.
//

#import "LMRegistController.h"

@interface LMRegistController ()
@property (weak, nonatomic) IBOutlet UITextField *emailTextField;
@property (weak, nonatomic) IBOutlet UITextField *unameTextField;

@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;


@end

@implementation LMRegistController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
- (IBAction)didClickRegsitAction:(UIButton *)sender {
    NSURL *url = [NSURL URLWithString:@"http://api2.pianke.me/user/reg"];
    NSMutableURLRequest *mRequest = [NSMutableURLRequest requestWithURL:url];
    [mRequest setHTTPMethod:@"POST"];
    //    设置请求体
    NSString *bodyStr = [NSString stringWithFormat:@"email=%@&gender=0&uname=%@&passwd=%@",self.emailTextField.text,self.unameTextField.text,self.passwordTextField.text];
    NSData *data = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
    
    [mRequest setHTTPBody:data];
    
    //    使用URLSession执行网络请求
    NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession]dataTaskWithRequest:mRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (data) {
            
            NSMutableDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSDictionary *dataDict = jsonDict[@"data"];
            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:dataDict[@"msg"]?dataDict[@"msg"]:@"注册成功" preferredStyle:UIAlertControllerStyleAlert];
            [alertC addAction:[UIAlertAction actionWithTitle:@"YES" style:UIAlertActionStyleDefault handler:nil]];
            
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [self presentViewController:alertC animated:YES completion:nil];
            });
        }
    }];
    [dataTask resume];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
